package com.yifeng.repo.controller.rocketmq.configure;

import com.yifeng.repo.controller.rocketmq.message.produce.OrderMessageQueueSelector;
import lombok.Data;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;
import java.util.Map;

/**
 * Created by daibing on 2023/8/5.
 */
@Data
@ConfigurationProperties(prefix = "yfcloud.controller.rocketmq")
public class RocketmqProperties {
    /**
     * 是否启用生产管理器
     */
    private boolean enableProduceManager = true;

    /**
     * 是否启用Pull消费管理器
     */
    private boolean enablePullConsumeManager = false;

    /**
     * 是否启用Pull消费指定队列管理器
     */
    private boolean enablePullConsumeAssignManager = false;

    /**
     * 是否启用Push消费管理器
     */
    private boolean enablePushConsumeManager = false;

    /**
     * 是否启用API接口
     */
    private boolean enableRocketmqApi = false;

    /**
     * 应用自己的topic（默认仅对应用自己的topic发布消息，对所有的topic都可以消费消息）
     */
    private String masterTopic;

    /**
     * 默认仅对应用自己的topic发布消息
     */
    private boolean masterTopicOnlyProduce = true;

    /**
     * 支持多个topic配置
     */
    private List<TopicProperties> topicPropertiesList;

    /**
     * 事务配置
     */
    private TxProduceProperties transaction;

    /**
     * 消息队列选择器
     */
    private Class<? extends MessageQueueSelector> customMessageQueueSelectorClass = OrderMessageQueueSelector.class;

    /**
     * api地址前缀
     */
    private String mqAdminPrefix = "/rocketmq";

    @Data
    public static class TopicProperties {
        /**
         * 基础配置
         */
        private String topic;
        private String namesrvAddr;
        private String accessKey;
        private String secretKey;
        private String producerGroup;
        private String consumerGroup;
        private Map<String, String> consumerGroupMapping;
        private ConsumeFromWhere consumeFromWhere = ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET;

        /**
         * 生产配置（字段定义同官方sdk）
         */
        private int sendMsgTimeout = 30 * 1000;
        private int retryTimesWhenSendFailed = 3;
        private int retryTimesWhenSendAsyncFailed = 3;
        private boolean retryAnotherBrokerWhenNotStoreOK = false;
        private int maxMessageSize = 1024 * 1024 * 4;
        private int compressMsgBodyOverHowmuch = 1024 * 4;

        /**
         * Pull模式线程配置
         */
        private int subscribePullThreadNums = 16;
        private int assignPullThreadNums = 2;
        private int pullBatchSize = 10;
        private boolean seekBackWhenError = false;

        /**
         * Push模式线程配置
         */
        private int pushConsumeThreadMin = 2;
        private int pushConsumeThreadMax = 16;
        private int pushBatchSize = 32;
        private int consumeMessageBatchMaxSize = 1;
        private int maxReconsumeTimes = 5;
    }

    @Data
    public static class TxProduceProperties {

        /**
         * 事务监听器
         */
        private Class<? extends TransactionListener> transactionListenerClass;

        /**
         * 线程配置
         */
        private int checkThreadPoolMinSize = 2;
        private int checkThreadPoolMaxSize = 4;
        private int checkRequestHoldMax = 128;

    }

}
